document.observe('dom:loaded', initCodebase);

var gxWindows = [];
var gxReportGenerated =[];
var gxReport = null;
var gnFiles = 0;
var gxTimeout = null;
var gnComments = 0;

function generateCodeReviewReport()
{
    gxWindows = [];
    gxReportGenerated = [];
    gnComments = 0;

    var files = document.querySelectorAll('.sourcefile'); //Codebase Files stored as links in HTML table
    gnFiles = files.length;

     if(!confirm("This is an experimental feature, and can take a long time to complete.\n"  +
        "Up to " + gnFiles + " browser tabs/windows will be opened and closed. Patience!!!")) return;

    if(gnFiles > 90){
        if(!confirm("This operation has been verified only with a maximum of 90 files.\n" +
            "Do you really want to proceed?"))
            return;
    }

    //Output browser window for report
    gxReport = window.open();
    gxReport.title = 'Neptuner Code Review Report';
    gxReport.document.open();
    gxReport.document.write(
        '<!DOCTYPE html PUBLIC"-// W3C//DTD XHTML 1.0 Transitional//EN"" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
        + "<html><head><title>Neptuner Code Review Report</title>"
        + "<link rel='stylesheet' type='text/css' media='all' href='uboat/submarine.css' />"
        + "<script type='text/javascript' src='uboat/prototype.js'></script>"
        + "<script type='text/javascript' src='uboat/fastinit.js'></script>"
        + "<script type='text/javascript' src='uboat/tablesort.js'></script>"
        + "</head><body><h1>Neptuner Code Review Report</h1><table id='crr' class='sortable'>"
        + "<thead><tr><th>File</th><th>Comment</th></tr></thead>"
        );

    for(var i = 0; i< gnFiles; i++){
        //Open each file in a browser window
        win = window.open(files[i].href);
        gxWindows.push(win);
        gxReportGenerated.push(null);
    }

    getCommentsFromAllWindows();
}

function getCommentsFromAllWindows(){
    var win = null;
    var i=0;

    for(i=0;i<gnFiles;i++){
        win = gxWindows[i];
        if(!win) continue;
        if(win.document.readyState === 'complete'){
            createReport(win);
            gxReportGenerated[i] = true;
            gxWindows[i]=null;
        }
    }

    for(i=0;i<gnFiles;i++){
        if(gxReportGenerated[i]!=true){
            if(gxTimeout) clearTimeout(gxTimeout);
            gxTimeout = setTimeout ( "getCommentsFromAllWindows()", 500);
            return;
        }
    }

    // All windows done
    if(gxTimeout) clearTimeout(gxTimeout);
    gxTimeout = null;
    gxReport.document.write("</table>");
    gxReport.document.write("<br/><b>" + gnComments + "</b> comments from <b>"+ gnFiles + "</b> files." + "</body></html>");
    gxReport.document.close();
}

function createReport(win){
    var comments = null;
    var filename =null;
    var target = null;

    comments = win.document.querySelectorAll('.review');
    filename = formatFilename(win.location.href);
    target = getRelativePath(win.location.href);

    for(var i=0;i<comments.length;i++){
        //Add to output report
        gxReport.document.writeln('<tr>');
        gxReport.document.writeln('<td>' + '<a href="'+ target + '#' + comments[i].id + '">'
        + filename + '</a></td>');
        gxReport.document.writeln('<td>' + comments[i].innerHTML + '</td>');
        gxReport.document.writeln('</tr>');
        gnComments++;
    }

    win.close();
}


function formatFilename(filename){
    var pos = filename.lastIndexOf(".uboat.html");
    if(pos > 0){
        filename = filename.substring(0, pos).toLowerCase();
    }

    pos = filename.lastIndexOf("codebase/");
    if(pos > 0){
        filename = filename.substring(pos + 9, filename.length).toLowerCase();
    }

    return filename;
}

function getRelativePath(sFullFilename){
    var pos = sFullFilename.lastIndexOf("codebase/");
    if(pos > 0){
        sFullFilename = sFullFilename.substring(pos, sFullFilename.length);
    }
    return sFullFilename;
}

function initCodebase(){
    $('menuNeptuner').setAttribute('title' , "Click to show/hide help.");

    $('usageHelp').innerHTML = 
        "<b>Usage Notes</b>"+
        "<li>Follow the links to browse the codebase files.</li>"+
        "<li>Left-click on icons to bring up menus.</li>"+
        "<li>Double-click on lines when they change color, to add content to them or edit them.</li>"+
        "<li>Click on titles of the columns in tables to sort them.</li>"+
        "<br/><i>(C)2009-2010 sonofdelphi/The Oraclique</i>"+
        "";
    $('usageHelp').hide();

    $('codePod').innerHTML =
        "<hr/>" +
        "<b>Codebase Name: </b>" + sCodebaseName + "<br/>" +
        "<b>Codebase Root: </b>" + sCodebaseRoot + "<br/>" +
        "<b>Include Root : </b>" + sIncludeRoot +"<br/>" +
        "<b>Files Parsed : </b>" + nFilesParsed + "<br/>" +
        "<input type='button' id=buttonCRRGen value='Generate Review Report'></input><br/>"+
        "<hr/>";

    $('codebaseDetails').insert({before:'<br/><b>Codebase Files</b><br/><br/>'});
    $('menuNeptuner').observe('click' , toggleUsageHelp);
    $('buttonCRRGen').observe('click' , generateCodeReviewReport);
    $('buttonCRRGen').insert({before:$('codebaseStats')});

}

function toggleUsageHelp(){
    $('usageHelp').toggle();
}